package com.whut.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whut.mapper.ContractchecktaskMapper;
import com.whut.mapper.ContracttaskprojectMapper;
import com.whut.mapper.MunicipalprojectinfoMapper;
import com.whut.mapper.NewProblemCollectDetailMapper;
import com.whut.model.*;
import com.whut.service.CheckInfoService;
import com.whut.service.ReportService;
//import com.whut.service.WHCJJService;
import com.whut.utils.FileUtil;
import com.whut.utils.HttpUtil;
import com.whut.utils.MDoc;
import com.whut.v_model.Municipalprojectinfo;
import com.whut.v_model.PCDandUSERNAME;
//import com.whut.v_model.TcProjectInfoWithBLOBs;
import com.whut.v_model.ProblemCollectDetail;
import com.whut.v_vo.*;
import freemarker.template.TemplateException;
import org.apache.http.client.ClientProtocolException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

@Controller
@RequestMapping("/WHCJJ")
public class WHCJJController {

//    @Resource
//    private WHCJJService WHCJJService;
@Resource
private ContracttaskprojectMapper contractTaskProjectMapper;
@Resource
private NewProblemCollectDetailMapper newProblemCollectDetailMapper;
@Resource
private ContractchecktaskMapper contractchecktaskMapper;
@Resource
private MunicipalprojectinfoMapper municipalprojectinfoMapper;
    /**
     * 市政数据对接
     * @param Type
     * @param projectCode
     * @return
     */
    @ResponseBody
    @RequestMapping( "/GetInfo")
    public Object getAllCheckInfo(String Type,String projectCode){
        List<Map<String, Object>> ResultList = new ArrayList<>();
        ContractTaskProject ctp = contractTaskProjectMapper.getProjectInfoByProjectCode(projectCode);
//        ContractTaskProject ctp = contractTaskProjectMapper.getProjectInfoByProjectId(projectCode);
        String projectName=ctp.getProjectName();
        int projectId=ctp.getProjectId();
        List<ProblemCollectDetail> pcd = newProblemCollectDetailMapper.getProblemCollectByProjectId(projectId);
        for (int i=0;i<pcd.size();i++){
            Map map = new LinkedHashMap<>();
            map.put("projectName",projectName);
            map.put("projectCode",projectCode);
            map.put("pName6",pcd.get(i).getpName6());
            map.put("checkPart",pcd.get(i).getCheckPart());
            map.put("problemDescription",pcd.get(i).getProblemDescription());
            map.put("remark",pcd.get(i).getRemark());
            map.put("problemQua",pcd.get(i).getProblemQua());
            map.put("riskPoint",pcd.get(i).getRiskPoint());
            map.put("picture",pcd.get(i).getPicName());
            map.put("code",pcd.get(i).getCode());
            String creator=contractchecktaskMapper.getCreatorByCheckId(pcd.get(i).getCheckId());
            map.put("userName",creator);
            map.put("checkTime",pcd.get(i).getCheckTime());
            ResultList.add(map);
        }
        if(Type.equals("0")){
            return ResultList;
        } else {
            return "ERROR";
        }
    }


    /**

     * @author wangqirui
     * @desc 更新市政数据到市政表和ctp表
     * @date 2021/8/11
     **/
    @ResponseBody
    @RequestMapping( "/UpdateSystemInfo")
    public void  UpdateSystemInfo(String ListData){
        /*插入市政表*/
        JSONArray projectInfo = JSON.parseArray(ListData);
        List<Municipalprojectinfo> listInfo = new ArrayList<>();
        for (int i = 0; i<projectInfo.size();i++){
            Municipalprojectinfo municipalprojectinfo = new Municipalprojectinfo();
            municipalprojectinfo= JSON.toJavaObject(projectInfo.getJSONObject(i),Municipalprojectinfo.class);
            municipalprojectinfo.setProjectName(projectInfo.getJSONObject(i).getString("name"));
            listInfo.add(municipalprojectinfo);
        }
        municipalprojectinfoMapper.insertOrUpdateInfoById(listInfo);

        /*插入ctp表*/
        for (int i = 0; i<projectInfo.size();i++) {
            ContractTaskProject ctpinfo = new ContractTaskProject();
            ctpinfo.setprojectCode(projectInfo.getJSONObject(i).getString("id"));
            ctpinfo.setProjectName(projectInfo.getJSONObject(i).getString("name"));
            ctpinfo.setProjectPrice(projectInfo.getJSONObject(i).getString("contractPrice"));
            ctpinfo.setArea(projectInfo.getJSONObject(i).getString("regionName"));
            ctpinfo.setProjectDescription(projectInfo.getJSONObject(i).getString("introduction"));
            ctpinfo.setSgUnit(projectInfo.getJSONObject(i).getString("implementationUnitName"));
            ctpinfo.setJsUnit(projectInfo.getJSONObject(i).getString("constructionUnitName"));
            ctpinfo.setJlUnit(projectInfo.getJSONObject(i).getString("supervisionUnitName"));
            if(projectInfo.getJSONObject(i).getString("lng").length()!=0){
                ctpinfo.setLongitude(Double.parseDouble(projectInfo.getJSONObject(i).getString("lng")));

            }
            if(projectInfo.getJSONObject(i).getString("lat").length()!=0){
            ctpinfo.setLatitude(Double.parseDouble(projectInfo.getJSONObject(i).getString("lat")));
            }
            ctpinfo.setContractId(67);
            ctpinfo.setTaskId(0);
            ContractTaskProject checkCTPInfo = contractTaskProjectMapper.getProjectInfoByProjectCode(ctpinfo.getprojectCode());
            if (checkCTPInfo != null) {
                ctpinfo.setProjectId(checkCTPInfo.getProjectId());
                contractTaskProjectMapper.updateProjectinfo(ctpinfo);
            } else contractTaskProjectMapper.insertInfo(ctpinfo);
        }

    }
}
